package wiki.hitime.demo.test.service;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import wiki.hitime.demo.test.bean.db.User;
import wiki.hitime.demo.test.bean.dto.ResultDTO;
import wiki.hitime.demo.test.bean.dto.UserDTO;
import wiki.hitime.demo.test.bean.dto.req.UserGetReqDTO;
import wiki.hitime.demo.test.enums.FinUserTypeEnum;
import wiki.hitime.demo.test.enums.UserTypeEnum;
import wiki.hitime.demo.test.feign.FinFeignClient;

import javax.annotation.Resource;

/**
 * @author zhangdaoping
 * @create 2019-10-31 13:38
 */
@Service
public class RpcBusinessService {

    @Resource
    private FinFeignClient finFeignClient;

    /**
     * 根据用户编号获取用户
     *
     * @param userId
     * @return
     */
    public UserDTO loadByUserId(String userId, Integer userType) {
        ResultDTO<User> resultDTO = finFeignClient.get(loadReq(userId, userType));
        if (null == resultDTO.getData()) {
            return null;
        }
        return convertDTO(resultDTO.getData());
    }

    private UserGetReqDTO loadReq(String userId, Integer userType) {
        FinUserTypeEnum finUserTypeEnum = loadFinUserTypeEnum(userType);
        return UserGetReqDTO.builder()
                .userId(userId)
                .userType(finUserTypeEnum.getStatus())
                .build();
    }

    private FinUserTypeEnum loadFinUserTypeEnum(Integer userType) {
        UserTypeEnum userTypeEnum = UserTypeEnum.getEnumByStatus(userType);
        Assert.notNull(userTypeEnum, "没有对应的用户类型");
        FinUserTypeEnum finUserTypeEnum = null;
        switch (userTypeEnum) {
            case NORMAL: {
                finUserTypeEnum = FinUserTypeEnum.NORMAL;
            }
            break;
            case OVERDUE: {
                finUserTypeEnum = FinUserTypeEnum.OVERDUE;
            }
            break;
            default:
        }
        return finUserTypeEnum;
    }

    private UserDTO convertDTO(User source) {
        return UserDTO.builder()
                .userId(source.getUserId())
                .userName(source.getUserName())
                .build();
    }

}
